/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.calendar.ZWeekDay;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import java.text.DateFormat;
/*     */ import java.util.Calendar;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.TimeZone;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FriendlyCalendaringDescription
/*     */ {
/*  49 */   static Log sLog = ZimbraLog.calendar;
/*     */   private boolean mDoneGenerate;
/*     */   private boolean mDefinitelyModified;
/*     */   private StringBuilder mPlainText;
/*     */   private StringBuilder mHtml;
/*     */   private final Account mAccount;
/*     */   private final Locale mLc;
/*     */   private final List<Invite> mInviteComponents;
/*     */   
/*     */   public FriendlyCalendaringDescription(List<Invite> inviteComponents, Account account) throws ServiceException {
/*  59 */     this.mDoneGenerate = false;
/*  60 */     this.mDefinitelyModified = false;
/*  61 */     this.mPlainText = null;
/*  62 */     this.mHtml = null;
/*  63 */     this.mAccount = account;
/*  64 */     this.mLc = this.mAccount.getLocale();
/*  65 */     this.mInviteComponents = inviteComponents;
/*     */   }
/*     */   
/*     */   public FriendlyCalendaringDescription(Invite invite, Account account) throws ServiceException {
/*  69 */     this(Lists.newArrayList(new Invite[] { invite }), account);
/*     */   }
/*     */   
/*     */   public void setDefinitelyModified(boolean val) {
/*  73 */     this.mDefinitelyModified = val;
/*     */   }
/*     */   
/*     */   public String getAsPlainText() {
/*  77 */     generateDescriptions();
/*  78 */     return this.mPlainText == null ? "" : this.mPlainText.toString();
/*     */   }
/*     */   
/*  81 */   public String getAsHtml() { generateDescriptions();
/*  82 */     return this.mHtml == null ? "" : this.mHtml.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void generateDescriptions()
/*     */   {
/*  89 */     if (this.mDoneGenerate)
/*  90 */       return;
/*  91 */     this.mDoneGenerate = true;
/*  92 */     if ((this.mInviteComponents == null) || (this.mInviteComponents.size() == 0))
/*  93 */       return;
/*  94 */     this.mPlainText = new StringBuilder();
/*  95 */     this.mHtml = new StringBuilder("<html>\n<body>\n");
/*  96 */     this.mHtml.append("<div style=\"font-family: monospace; font-size: 14px\">\n");
/*  97 */     Invite invite = (Invite)this.mInviteComponents.get(0);
/*  98 */     if (invite.isEvent())
/*  99 */       addEventDetails(invite);
/* 100 */     String description = null;
/*     */     try {
/* 102 */       description = invite.getDescription();
/*     */     } catch (ServiceException e) {
/* 104 */       sLog.debug("Resetting descriptions due to ServiceException", e);
/* 105 */       description = null;
/*     */     }
/* 107 */     if (description != null) {
/* 108 */       this.mPlainText.append(description).append("\n");
/* 109 */       this.mHtml.append("<pre>").append(description).append("</pre>\n");
/*     */     }
/* 111 */     this.mHtml.append("</div>\n");
/* 112 */     this.mHtml.append("</body>\n</html>\n");
/*     */   }
/*     */   
/*     */   private void addEventDetails(Invite invite) {
/* 116 */     if (!invite.isEvent())
/* 117 */       return;
/* 118 */     int origPlainLen = this.mPlainText.length();
/*     */     try {
/* 120 */       String uid = invite.getUid();
/* 121 */       String method = invite.getMethod();
/* 122 */       List<ZAttendee> attendees = invite.getAttendees();
/* 123 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(this.mAccount);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 128 */       CalendarItem calItem = mbox.getCalendarItemByUid(null, uid);
/* 129 */       if (calItem != null) {
/* 130 */         Invite[] calInvites = calItem.getInvites();
/* 131 */         if ((calInvites != null) && (calInvites.length > 1))
/* 132 */           this.mDefinitelyModified = true;
/*     */       }
/* 134 */       L10nUtil.MsgKey hdrKey = null;
/* 135 */       if (method.equals("REQUEST")) {
/* 136 */         if (invite.getRecurId() != null) {
/* 137 */           hdrKey = L10nUtil.MsgKey.zsApptInstanceModified;
/* 138 */         } else if (this.mDefinitelyModified) {
/* 139 */           hdrKey = L10nUtil.MsgKey.zsApptModified;
/*     */         } else
/* 141 */           hdrKey = L10nUtil.MsgKey.zsApptNew;
/* 142 */       } else if (method.equals("CANCEL")) {
/* 143 */         if (invite.getRecurId() != null) {
/* 144 */           hdrKey = L10nUtil.MsgKey.calendarCancelAppointmentInstance;
/*     */         } else
/* 146 */           hdrKey = L10nUtil.MsgKey.calendarCancelAppointment;
/* 147 */       } else if ((method.equals("REPLY")) && 
/* 148 */         (attendees != null) && (!attendees.isEmpty())) {
/* 149 */         ZAttendee replier = (ZAttendee)attendees.get(0);
/* 150 */         String partStat = replier.getPartStat();
/* 151 */         String replierName = replier.getFriendlyAddress().toString();
/* 152 */         if (partStat.equals("AC")) {
/* 153 */           hdrKey = L10nUtil.MsgKey.calendarDefaultReplyAccept;
/* 154 */         } else if (partStat.equals("TE")) {
/* 155 */           hdrKey = L10nUtil.MsgKey.calendarDefaultReplyAccept;
/* 156 */         } else if (partStat.equals("DE")) {
/* 157 */           hdrKey = L10nUtil.MsgKey.calendarDefaultReplyDecline;
/*     */         }
/* 159 */         if (hdrKey != null) {
/* 160 */           this.mPlainText.append(L10nUtil.getMessage(hdrKey, this.mLc, new Object[] { replierName })).append("\n\n");
/* 161 */           this.mHtml.append("<h3>").append(L10nUtil.getMessage(hdrKey, this.mLc, new Object[] { replierName })).append("</h3>");
/* 162 */           hdrKey = null;
/*     */         }
/*     */       }
/*     */       
/* 166 */       if (hdrKey != null) {
/* 167 */         this.mPlainText.append(L10nUtil.getMessage(hdrKey, this.mLc, new Object[0])).append("\n\n");
/* 168 */         this.mHtml.append("<h3>").append(L10nUtil.getMessage(hdrKey, this.mLc, new Object[0])).append("</h3>");
/*     */       }
/* 170 */       this.mHtml.append("\n\n<p>\n<table border=\"0\">\n");
/*     */       
/* 172 */       addSimpleRow(L10nUtil.MsgKey.zsSubject, invite.getName());
/* 173 */       if (invite.hasOrganizer()) {
/* 174 */         addSimpleRow(L10nUtil.MsgKey.zsOrganizer, invite.getOrganizer().getFriendlyAddress().toString());
/*     */       }
/* 176 */       this.mHtml.append("</table>\n<p>\n<table border=\"0\">\n");
/* 177 */       addSimpleRow(L10nUtil.MsgKey.zsLocation, invite.getLocation());
/*     */       
/* 179 */       ParsedDateTime startTime = invite.getStartTime();
/* 180 */       if (startTime != null) {
/* 181 */         ParsedDateTime endTime = invite.getEndTime();
/* 182 */         if (endTime == null) {
/* 183 */           ParsedDuration dur = invite.getDuration();
/* 184 */           if (dur == null) {
/* 185 */             dur = ParsedDuration.parse(false, 0, 0, 1, 0, 0);
/*     */           }
/* 187 */           endTime = startTime.add(dur);
/*     */         }
/* 189 */         addSimpleRow(L10nUtil.MsgKey.zsTime, getTimeDisplayString(startTime, endTime, invite.isRecurrence(), invite.isAllDayEvent()));
/*     */         
/* 191 */         ZRecur zr = getRecur(invite);
/* 192 */         if (zr != null) {
/* 193 */           addSimpleRow(L10nUtil.MsgKey.zsRecurrence, getRecurrenceDisplayString(zr, startTime.getCalendarCopy(), this.mLc));
/*     */         }
/*     */       }
/*     */       
/* 197 */       if ((!method.equals("REPLY")) && (attendees != null) && (!attendees.isEmpty())) {
/* 198 */         this.mHtml.append("</table>\n<p>\n<table border=\"0\">\n");
/* 199 */         StringBuilder attendeeList = new StringBuilder();
/* 200 */         boolean firstAdded = false;
/* 201 */         for (ZAttendee attendee : attendees) {
/* 202 */           if (firstAdded) {
/* 203 */             attendeeList.append(", ");
/*     */           } else {
/* 205 */             firstAdded = true;
/*     */           }
/* 207 */           attendeeList.append(attendee.getFriendlyAddress().toString());
/*     */         }
/* 209 */         addSimpleRow(L10nUtil.MsgKey.zsInvitees, attendeeList.toString());
/*     */       }
/* 211 */       this.mPlainText.append("*~*~*~*~*~*~*~*~*~*\n");
/* 212 */       this.mHtml.append("</table>\n");
/* 213 */       this.mHtml.append("<div>*~*~*~*~*~*~*~*~*~*</div><br>\n");
/*     */     } catch (ServiceException e) {
/* 215 */       sLog.debug("Resetting descriptions due to ServiceException", e);
/* 216 */       this.mPlainText.setLength(origPlainLen);
/* 217 */       this.mHtml.setLength(0);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addSimpleRow(L10nUtil.MsgKey msgKey, String value)
/*     */   {
/* 227 */     if ((value == null) || (value.length() == 0))
/* 228 */       return;
/* 229 */     String key = L10nUtil.getMessage(msgKey, this.mLc, new Object[0]);
/* 230 */     this.mPlainText.append(key).append(": ").append(value).append("\n");
/* 231 */     this.mHtml.append("<tr><th align=\"left\">");
/* 232 */     this.mHtml.append(StringUtil.escapeHtml(key));
/* 233 */     this.mHtml.append(":</th><td>");
/* 234 */     this.mHtml.append(StringUtil.escapeHtml(value));
/* 235 */     this.mHtml.append("</td></tr>\n");
/*     */   }
/*     */   
/*     */   public String getTimeDisplayString(ParsedDateTime start, ParsedDateTime end, boolean isRecurrence, boolean isAllDayEvent) throws ServiceException
/*     */   {
/* 240 */     return getTimeDisplayString(start, end, isRecurrence, isAllDayEvent, this.mLc, this.mAccount);
/*     */   }
/*     */   
/*     */   public static String getTimeDisplayString(ParsedDateTime start, ParsedDateTime end, boolean isRecurrence, boolean isAllDayEvent, Locale locale, Account account) throws ServiceException
/*     */   {
/* 245 */     StringBuilder sb = new StringBuilder();
/*     */     TimeZone timeZone;
/* 247 */     TimeZone timeZone; if (start.getTimeZone() != null) {
/* 248 */       timeZone = start.getTimeZone();
/*     */     } else
/* 250 */       timeZone = Util.getAccountTimeZone(account);
/* 251 */     if (!isRecurrence) {
/* 252 */       DateFormat df = DateFormat.getDateInstance(0, locale);
/* 253 */       df.setTimeZone(timeZone);
/* 254 */       sb.append(df.format(start.getDate())).append(", ");
/*     */     }
/* 256 */     if (isAllDayEvent) {
/* 257 */       sb.append(L10nUtil.getMessage(L10nUtil.MsgKey.zsAllDay, locale, new Object[0]));
/*     */     } else {
/* 259 */       DateFormat timeFormat = DateFormat.getTimeInstance(1, locale);
/* 260 */       timeFormat.setTimeZone(timeZone);
/* 261 */       sb.append(timeFormat.format(start.getDate())).append(" - ");
/* 262 */       if (!isRecurrence) {
/* 263 */         Calendar calStart = start.getCalendarCopy();
/* 264 */         Calendar calEnd = end.getCalendarCopy();
/* 265 */         boolean isSameday = (calStart.get(1) == calEnd.get(1)) && (calStart.get(2) == calEnd.get(2)) && (calStart.get(5) == calEnd.get(5));
/*     */         
/*     */ 
/* 268 */         if (!isSameday) {
/* 269 */           DateFormat df = DateFormat.getDateInstance(0, locale);
/* 270 */           df.setTimeZone(timeZone);
/* 271 */           sb.append(df.format(end.getDate())).append(", ");
/*     */         }
/*     */       }
/* 274 */       sb.append(timeFormat.format(end.getDate()));
/*     */     }
/* 276 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public static String getRecurrenceDisplayString(ZRecur zr, Calendar dtStart, Locale lc) {
/* 280 */     String repeat = null;
/* 281 */     if (zr.getFrequency().equals(ZRecur.Frequency.DAILY)) {
/* 282 */       List<ZRecur.ZWeekDayNum> dayList = zr.getByDayList();
/* 283 */       if ((dayList != null) && (!dayList.isEmpty())) {
/* 284 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurDailyEveryWeekday, lc, new Object[0]);
/* 285 */       } else if (zr.getInterval() > 1) {
/* 286 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurDailyEveryNumDays, lc, new Object[] { Integer.valueOf(zr.getInterval()) });
/*     */       } else {
/* 288 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurDailyEveryDay, lc, new Object[0]);
/*     */       }
/* 290 */     } else if (zr.getFrequency().equals(ZRecur.Frequency.WEEKLY)) {
/* 291 */       List<ZRecur.ZWeekDayNum> dayList = zr.getByDayList();
/* 292 */       int dayOfWeek = 0;
/* 293 */       if ((dayList == null) || (dayList.isEmpty())) {
/* 294 */         dayOfWeek = dtStart.get(7);
/*     */       } else {
/* 296 */         dayOfWeek = ((ZRecur.ZWeekDayNum)dayList.get(0)).mDay.getCalendarDay();
/*     */       }
/* 298 */       Calendar cal = Calendar.getInstance(lc);
/* 299 */       cal.set(7, dayOfWeek);
/* 300 */       if (zr.getInterval() == 1) {
/* 301 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurWeeklyEveryWeekday, lc, new Object[] { cal.getTime() });
/*     */       } else {
/* 303 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurWeeklyEveryNumWeeksDate, lc, new Object[] { Integer.valueOf(zr.getInterval()), cal.getTime() });
/*     */       }
/* 305 */     } else if (zr.getFrequency().equals(ZRecur.Frequency.MONTHLY)) {
/* 306 */       List<Integer> monthdayList = zr.getByMonthDayList();
/* 307 */       if ((monthdayList != null) && (!monthdayList.isEmpty())) {
/* 308 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurMonthlyEveryNumMonthsDate, lc, new Object[] { monthdayList.get(0), Integer.valueOf(zr.getInterval()) });
/*     */       } else {
/* 310 */         List<ZRecur.ZWeekDayNum> weekdayList = zr.getByDayList();
/* 311 */         if ((weekdayList != null) && (!weekdayList.isEmpty())) {
/* 312 */           Calendar cal = Calendar.getInstance(lc);
/* 313 */           cal.set(7, ((ZRecur.ZWeekDayNum)weekdayList.get(0)).mDay.getCalendarDay());
/* 314 */           repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurMonthlyEveryNumMonthsNumDay, lc, new Object[] { Integer.valueOf(weekOfMonth(zr)), cal.getTime(), Integer.valueOf(zr.getInterval()) });
/*     */         } else {
/* 316 */           repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurMonthlyEveryNumMonthsDate, lc, new Object[] { Integer.valueOf(dtStart.get(5)), Integer.valueOf(zr.getInterval()) });
/*     */         }
/*     */       }
/* 319 */     } else if (zr.getFrequency().equals(ZRecur.Frequency.YEARLY)) {
/* 320 */       Calendar cal = Calendar.getInstance(lc);
/* 321 */       List<Integer> monthList = zr.getByMonthList();
/* 322 */       int month = 0;
/* 323 */       if ((monthList != null) && (!monthList.isEmpty())) {
/* 324 */         month = ((Integer)monthList.get(0)).intValue() - 1;
/*     */       } else {
/* 326 */         month = dtStart.get(5);
/*     */       }
/* 328 */       cal.set(2, month);
/* 329 */       List<Integer> monthdayList = zr.getByMonthDayList();
/* 330 */       if ((monthdayList != null) && (!monthdayList.isEmpty())) {
/* 331 */         repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurYearlyEveryDate, lc, new Object[] { cal.getTime(), monthdayList.get(0) });
/*     */       } else {
/* 333 */         List<ZRecur.ZWeekDayNum> weekdayList = zr.getByDayList();
/* 334 */         if ((weekdayList != null) && (!weekdayList.isEmpty())) {
/* 335 */           cal.set(7, ((ZRecur.ZWeekDayNum)weekdayList.get(0)).mDay.getCalendarDay());
/* 336 */           repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurYearlyEveryMonthNumDay, lc, new Object[] { Integer.valueOf(weekOfMonth(zr)), cal.getTime(), cal.getTime() });
/*     */         } else {
/* 338 */           repeat = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurYearlyEveryDate, lc, new Object[] { cal.getTime(), Integer.valueOf(dtStart.get(5)) });
/*     */         }
/*     */       }
/*     */     }
/* 342 */     else if (!$assertionsDisabled) { throw new AssertionError();
/*     */     }
/*     */     
/* 345 */     String start = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurStart, lc, new Object[] { dtStart.getTime() });
/*     */     
/* 347 */     String end = null;
/* 348 */     if (zr.getCount() > 0) {
/* 349 */       end = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurEndNumber, lc, new Object[] { Integer.valueOf(zr.getCount()) });
/* 350 */     } else if (zr.getUntil() != null) {
/* 351 */       end = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurEndByDate, lc, new Object[] { zr.getUntil().getDate() });
/*     */     } else {
/* 353 */       end = L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurEndNone, lc, new Object[0]);
/*     */     }
/*     */     
/* 356 */     return L10nUtil.getMessage(L10nUtil.MsgKey.zsRecurBlurb, lc, new Object[] { repeat, start, end });
/*     */   }
/*     */   
/*     */   public static ZRecur getRecur(Invite invite) {
/* 360 */     ZRecur zr = null;
/*     */     
/* 362 */     Recurrence.IRecurrence r = invite.getRecurrence();
/* 363 */     if (r != null)
/*     */     {
/*     */ 
/* 366 */       assert (r.getType() == 1);
/* 367 */       Recurrence.RecurrenceRule masterRule = (Recurrence.RecurrenceRule)r;
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 372 */       Iterator<Recurrence.IRecurrence> iter = masterRule.addRulesIterator(); if ((iter != null) && (iter.hasNext())) {
/* 373 */         r = (Recurrence.IRecurrence)iter.next();
/*     */         
/* 375 */         switch (r.getType())
/*     */         {
/*     */         case 6: 
/* 378 */           if (!$assertionsDisabled) throw new AssertionError();
/*     */           break;
/*     */         case 5: 
/* 381 */           Recurrence.SimpleRepeatingRule srr = (Recurrence.SimpleRepeatingRule)r;
/* 382 */           zr = srr.getRule();
/* 383 */           break;
/*     */         default: 
/* 385 */           if (!$assertionsDisabled) { throw new AssertionError();
/*     */           }
/*     */           break;
/*     */         }
/* 389 */         assert (!iter.hasNext());
/*     */       }
/*     */       
/* 392 */       assert (zr != null);
/*     */     }
/*     */     
/* 395 */     return zr;
/*     */   }
/*     */   
/*     */   public static int weekOfMonth(ZRecur recur) {
/* 399 */     List<ZRecur.ZWeekDayNum> weekdayList = recur.getByDayList();
/* 400 */     assert (weekdayList.size() == 1);
/* 401 */     int offset = ((ZRecur.ZWeekDayNum)weekdayList.get(0)).mOrdinal;
/* 402 */     if (offset == 0) {
/* 403 */       List<Integer> setposList = recur.getBySetPosList();
/* 404 */       if ((setposList != null) && (!setposList.isEmpty())) {
/* 405 */         assert (setposList.size() == 1);
/* 406 */         offset = ((Integer)setposList.get(0)).intValue();
/*     */       }
/*     */     }
/* 409 */     switch (offset) {
/*     */     case -1: case 1: case 2: case 3: case 4: 
/*     */       break;
/*     */     case 0: default: 
/* 413 */       sLog.warn("Invalid RECUR pattern: " + recur.toString());
/*     */     }
/*     */     
/* 416 */     return offset;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/FriendlyCalendaringDescription.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */